package common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public  class OrderUtil {
	
	/**
	 * 定位支付--B
	 */
	public final static String SORDER_BUY  = "B";
	/**
	 * 推送支付 --p
	 */
	public final static String ORDER_PUSH  = "P";
	/**
	 * 当面支付 --u
	 */
	public final static String ORDER_USE   = "U";
	/**
	 * 账单支付 --z
	 */
	public final static String ORDER_ZHANGDAN   = "Z";
	
	/**
	 * 活动订单支付 --H
	 */
	public final static String ORDER_ACTIVITY   = "H";
	
	/**
	 * 中国建设教育在线系统 默认的用户ID -- 100000000L
	 */
	public final static Long CUSTUMER_DEFUALT   = 100000000L;

	/**
	 * Method Name: makeOrderNumber
	 * Description: 生成定单号
	 * @param sType: 订单类型
	 * @param userId:用户ID
	 * @param maxLength:用户每日订单数
	 * @return 20位定单号
	 * @version 1.0
	 * @author 陈家德
	 * @date 2014/09/10
	 * @
	 */
	public static String  makeOrderNumber(String sType,Long userId,int maxLength){
		String sMaxlen = "0000"+(maxLength+1);
		return sType + getDateFromatString() + userId + sMaxlen.substring(sMaxlen.length()-4, sMaxlen.length());
	}
	/**
	 * Method Name: getDateFromatString
	 * Description: 取得当前日期格式字符串（YYYYMMDD）
	 * @return 格式字符串
	 * @version 1.0
	 * @author 陈家德
	 * @date 2014/09/10
	 */
	public static String getDateFromatString(){
		DateFormat format = new SimpleDateFormat("yyMMdd"); 
		return format.format(Calendar.getInstance().getTime());
	}

	/**
	 * Test
	 * @param args
	 */
	public static void main(String[] args){
		System.out.print(makeOrderNumber("S",123122221L,6123));
	}
	
	/**
	 * 生成字母和数字混合的n位随机数
	 *
	 * @param length
	 * @return
	 */
	public static String generateString(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random(System.currentTimeMillis());
		String charStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (int i = 0; i < length; i++) {
			sb.append(charStr.charAt(random.nextInt(charStr.length())));
		}
		return sb.toString();
	}
	
	/**
	 * 生成数字混合的n位随机数
	 *
	 * @param length
	 * @return
	 */
	public static String generateNumberString(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random(System.currentTimeMillis());
		String charStr = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (int i = 0; i < length; i++) {
			sb.append(charStr.charAt(random.nextInt(charStr.length())));
		}
		return sb.toString();
	}
	
	/**
	 * 生成数字混合的n位随机数
	 *
	 * @param length
	 * @return
	 */
	public static String generateNumbers(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random(System.currentTimeMillis());
		String charStr = "0123456789";
		for (int i = 0; i < length; i++) {
			sb.append(charStr.charAt(random.nextInt(charStr.length())));
		}
		return sb.toString();
	}
	
	/**
	 * 商户id不足15位补0
	 * @param id
	 * @return
	 */
	public static String getMerchantLongId(Long id){
		if(id == null) return "0";
		String pattern="000000000000000";
		java.text.DecimalFormat df = new java.text.DecimalFormat(pattern);
		return df.format(id);
	}

}
